#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This file is part of Beremiz, a Integrated Development Environment for
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
#
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
#
# See COPYING file for copyrights details.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


import wx
import wx.grid


class SelectionCellControl(wx.Control):
    """Custom cell editor control with a text box and a button that launches the
    SelectionEditorDialog.
    """

    def __init__(self, parent, options):
        """
        Args:
            parent:
            options:
        """
        wx.Control.__init__(self, parent)

        main_sizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=0)
        main_sizer.AddGrowableCol(0)
        main_sizer.AddGrowableRow(0)
        self.Selection = wx.Choice(self, choices=options)
        self.Selection.Bind(wx.EVT_CHOICE, self.OnSelectionChar)
        main_sizer.Add(self.Selection, flag=wx.GROW)
        # create browse button

        self.Bind(wx.EVT_SIZE, self.OnSize)

        self.SetSizer(main_sizer)

        self.Default = None
        self.Bind(wx.EVT_MOUSE_CAPTURE_LOST, self.onEVT_MOUSE_CAPTURE_LOST)

    def onEVT_MOUSE_CAPTURE_LOST(self, event):
        """
        Args:
            event:
        """
        if self.HasCapture():
            self.ReleaseMouse()

    def SetValue(self, value):
        """
        Args:
            value:
        """
        self.Default = value
        self.Selection.SetSelection(self.Selection.FindString(value))

    def GetValue(self):
        return self.Selection.GetString(self.Selection.GetSelection())

    def OnSize(self, event):
        """
        Args:
            event:
        """
        self.Layout()

    def OnSelectionChar(self, event):
        """
        Args:
            event:
        """
        wx.CallAfter(self.Parent.Parent.CloseEditControl)
        self.Selection.SetFocus()

    def SetFocus(self):
        self.Selection.SetFocus()


class SelectionCellEditor(wx.grid.GridCellEditor):
    """Grid cell editor that uses SelectionCellControl to display an edit
    button.
    """

    def __init__(self, table, row, colname, options):
        """
        Args:
            table:
            row:
            colname:
            options:
        """
        wx.grid.GridCellEditor.__init__(self)

        self.Table = table
        self.Colname = colname
        self.options = options

    def __del__(self):
        self.CellControl = None

    def Create(self, parent, id, evt_handler):
        """
        Args:
            parent:
            id:
            evt_handler:
        """
        self.CellControl = SelectionCellControl(parent, self.options)
        self.SetControl(self.CellControl)
        if evt_handler:
            self.CellControl.PushEventHandler(evt_handler)
            self.CellControl.Bind(wx.EVT_WINDOW_DESTROY, self.onEVT_CLOSE)

    def onEVT_CLOSE(self, event):
        """
        Args:
            event:
        """
        self.CellControl.PopEventHandler()

    def BeginEdit(self, row, col, grid):
        """
        Args:
            row:
            col:
            grid:
        """
        self.CellControl.Enable()
        self.CellControl.SetValue(self.Table.GetValueByName(row, self.Colname))
        self.CellControl.SetFocus()

    def EndEdit(self, row, col, grid, oldval):
        """
        Args:
            row:
            col:
            grid:
            oldval:
        """
        Selection = self.CellControl.GetValue()
        changed = Selection != oldval
        self.CellControl.Disable()
        return changed

    def ApplyEdit(self, row, col, grid):
        """
        Args:
            row:
            col:
            grid:
        """
        loc = self.CellControl.GetValue()
        self.Table.SetValue(row, col, loc)

    def SetSize(self, rect):
        """
        Args:
            rect:
        """
        self.CellControl.SetSize(rect.x + 1, rect.y,
                                 rect.width, rect.height,
                                 wx.SIZE_ALLOW_MINUS_ONE)

    def Clone(self):
        return SelectionCellEditor(self.Table, self.Colname)

    def SetParameters(self, value):
        """
        Args:
            value:
        """
        pass
